<?php
/**
 * Admin utils
 *
 * @author Anton
 */
class Lemar_Recepient
{
	
//----------------------------------------------------------------------------------------------------
	
	public static function saveMulti(array $data)
    {
    	if (count($data) > 0)
    	{
	    	foreach ($data as $d)
	    	{
		    	if ( self::validateEmail($d['address'], $d['id_campaign']) === true )
				{
					// Getting a random message data
					$message = Lemar_Message::getRandomByIdCampaign($d['id_campaign']);
					$emailData = Lemar_Message::previewMessage($message['id'], $d['id_campaign']);
					
					$id_address = models_AddressMapper::save($d);
					
					$dataStatistic = array(
						'id_campaign' => $d['id_campaign'],
						'id_from' => $emailData['from']->id,
						'id_message' => $message['id'],
						'id_subject' => $emailData['subject']->id,
						'id_address' => $id_address
					);
					$id_statistic = models_StatisticMapper::save($dataStatistic);
					
					$dataQueue = array(
						'address' => $d['address'],
						'message' => $emailData['body']->body,
						'subject' => $emailData['subject']->subject,
						'from' => $emailData['from']->from,
						'from_name' => $emailData['from']->name,
						'id_statistic' => $id_statistic
					);
					models_MessagequeMapper::save($dataQueue);
				}
	    	}
    	}
    }
    
//----------------------------------------------------------------------------------------------------
	
	public static function validateEmail($email , $id_campaign)
    {
    	$email = trim($email);
    	$id_campaign = (int) $id_campaign;
    	$validatorEmail = new Zend_Validate_EmailAddress();
		if ($email)
		{
			if ($validatorEmail->isValid($email))
			{
				// Email appears to be valid
				
				if ( !models_AddressMapper::inCampaign($email, $id_campaign) )
				{
					// Email is not in campaign
					$message = Lemar_Message::getRandomByIdCampaign($id_campaign);
					
					if ($message['id'])
					{
						// Campaign has at least one message
						$subject = models_MessagesubjectMapper::getRandom($message['id']);
						
						if ($subject->id)
						{
							// Message has at least one subject
							$from = models_CampaignfromMapper::getRandom($id_campaign);
							
							if ($from['id'])
							{
								// Campaign has at least one 'From'
								return true;
							}
							else
							{
								return "E-mail '".$email."' will not be added to the Campaign (id: ".$id_campaign."), because there are no 'From' in Campaign.";
							}
						}
						else
						{
							return "E-mail '".$email."' will not be added to the Campaign (id: ".$id_campaign."), because there are no Subjects for e-mail messages.";
						}
					}
					else
					{
						return "E-mail '".$email."' will not be added to the Campaign (id: ".$id_campaign."), because campaign has no Messages.";
					}
				}
				else
				{
					return "E-mail '".$email."' will not be added to the Campaign (id: ".$id_campaign."), because it is already in Campaign.";
				}
			}
			else
			{
				// email is invalid
				$messages = '';
				foreach ($validatorEmail->getMessages() as $messageId => $message)
				{
					$messages .= " $message<br />";
				}
				return $messages." E-mail '".$email."' will not be added.";
			}
		}
		else
		{
			return false;
		}
    }
    
//----------------------------------------------------------------------------------------------------
    
}